// pages/addlist/addlist.js
Page({

  /**
   * 页面的初始数据
   * vevent.name 尽量保持在15个汉字以内
   */
  data: {
    array: ['事件1'],
    vevent: [{'color': 'red', 'name': '完成小程序的制作','label':'个人','time':'07:50'},
      { 'color': '#FF6600', 'name': '努力学习英语', 'label': '学习', 'time': '07:50' }, 
      { 'color': '#FFFF33', 'name': '完成小程序的制作', 'label': '个人', 'time': '07:50' },
      { 'color': '#00FF00', 'name': '哈尔滨奥斯卡今儿个欧式机苏东坡', 'label': '其它', 'time': '07:50'}],
    date:'',
    delBtnWidth: 120,
    startX: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var vdate = new Date();
    this.setData({
      date: '今天\t' + vdate.toLocaleDateString()
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * Switch开关改变状态 
   */ 
  switchChange: function (e) {
    const flag = e.detail.value;
    console.log('switch1 发生 change 事件，携带值为', flag);
    if (flag){
      this.setData({
        date: '今天'
      });
      this.addobj();
    }else{
      this.setData({
        date: '未来7天'
      })
      this.buildevent();
    }
  },


//addobj
 addobj :function(){
   var obj = { 'color': '#FF6600', 'name': '努力学习英语', 'label': '学习', 'time': '07:50' };
   this.data.vevent.push(obj);
   var arr = this.data.vevent;
//console.log(arr);
   this.setData({
     vevent: arr
   })
  },

//校课表
//buildevent
buildevent : function(){
  var tt = [
    {
      "kcmc": "通信原理", "kcbh": "0700320",
      "jxbmc": "150805,150806", "kcrwdm": "1029638",
      "jcdm2": "01,02", "zcs": "7,15,14,11,9,8,6,16,13,12,10",
      "xq": "2", "jxcdmcs": "黄浩川教学楼403",
      "teaxms": "杨芷华"
    }
  ];
  console.log(tt[0].kcmc);
},

/*-------------------------------------*/
//
touchS: function (e) {
  //console.log("touchStart");
  if (e.touches.length == 1) {
    this.setData({
      startX: e.touches[0].clientX
    });
  }
},
//
touchM: function (e) {
  //console.log(e);
  var that = this;
  if (e.touches.length == 1) {
    var moveX = e.touches[0].clientX;
    var disX = that.data.startX - moveX;
  //console.log(that.data.startX+"--moveX:"+moveX)
    var delBtnWidth = that.data.delBtnWidth;
    var txtStyle = "";
 //console.log("touchM:"+disX);
    if (disX == 0 || disX < 0) {
      txtStyle = "left:0px";
    } else if (disX > 0) {
      txtStyle = "left:-" + disX + "px";
      if (disX >= delBtnWidth) {
        txtStyle = "left:-" + delBtnWidth + "px";
      }
    }
  }
  //console.log(e);
  var index = e.currentTarget.dataset.index;
  var list = that.data.vevent;

  list[index].txtStyle = txtStyle;

  this.setData({
    vevent: list
  });
},
//
touchE: function (e) {
  //console.log(e);
  var that = this;
  if (e.changedTouches.length == 1) {
    var endX = e.changedTouches[0].clientX;
    var disX = that.data.startX - endX;
    var delBtnWidth = that.data.delBtnWidth;
    var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";

    var index = e.currentTarget.dataset.index;
    var list = that.data.vevent;

  //console.log(txtStyle);
    //console.log(e);
    list[index].txtStyle = txtStyle;
    that.setData({
      vevent: list
    });
  }
},
//
deladdr: function (e) {
  //console.log(e);
  var index = e.target.dataset.index;
  var list = this.data.vevent;
  list.splice(index, 1);
  //console.log(list);
  this.setData({
    vevent: list
  });
}

})